package org.cjlm.ui.pub;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;

public class HeadItem extends JPanel {
    private String code;
    private String name;

    MLabel label;
    MTextField textField;
    public HeadItem(String code, String name) {
        this.code = code;
        this.name = name;
        setLayout(new GridBagLayout());
//        setBorder(new LineBorder(Color.BLUE, 2, true));//蓝色边框
        setMaximumSize(new  Dimension(335, 30));

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets  = new Insets(2, 5, 2, 5); // 组件间距 (上,左,下,右)

        // 固定大小的标签
        label = new MLabel(name);
        label.setHorizontalAlignment(SwingConstants.RIGHT);

        // 1. Label 设置 (左侧小面积)
        gbc.gridx  = 0;
        gbc.gridy  = 0;
        gbc.weightx  = 0.1; // 仅占10%宽度
        gbc.fill  = GridBagConstraints.NONE; // 不拉伸
        gbc.anchor  = GridBagConstraints.LINE_END; // 文本右对齐
        add(label,  gbc);

        textField = new MTextField();

        // 2. TextField 设置 (右侧大面积)
        gbc.gridx  = 1;
        gbc.weightx  = 0.9; // 占90%宽度
        gbc.fill  = GridBagConstraints.HORIZONTAL; // 水平拉伸填充
        add(textField,  gbc);
    }

    public void setEditable(boolean editable){
        textField.setEditable(editable);
    }

    public void setTxtBackground(Color c){
        textField.setBackground(c);
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public void setName(String name) {
        this.name = name;
    }
}
